package scau.redi.social_platform.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import scau.redi.social_platform.common.staticCode.Const;
import scau.redi.social_platform.common.util.ResponsePageUtil;
import scau.redi.social_platform.dao.*;
import scau.redi.social_platform.domain.pojo.Shared;
import scau.redi.social_platform.domain.pojo.SharedComment;
import scau.redi.social_platform.domain.pojo.SharedLiked;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.domain.vo.BriefSharedVO;
import scau.redi.social_platform.domain.vo.CommentTreeVO;
import scau.redi.social_platform.domain.vo.DetailSharedVO;
import scau.redi.social_platform.service.SharedService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author redi
 * @since 2022-05-05
 */
@Service
public class SharedServiceImpl implements SharedService {

    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SharedMapper sharedMapper;
    @Autowired
    SharedContentMapper sharedContentMapper;
    @Autowired
    SharedImgMapper sharedImgMapper;
    @Autowired
    SharedCollectionMapper sharedCollectionMapper;
    @Autowired
    SharedLikedMapper sharedLikedMapper;
    @Autowired
    SharedCommentMapper sharedCommentMapper;

    @Override
    public int createShared(Shared shared) {
        if (sharedMapper.insertSelective(shared)>0){
            return sharedMapper.selectLastInsertId();
        }
        return 0;
    }

    @Override
    public int deleteShared(int sharedId) {
        return sharedMapper.deleteByPrimaryKey(sharedId);
    }

    @Override
    public int updateShared(Shared shared) {
        return sharedMapper.updateByPrimaryKeySelective(shared);
    }

    @Override
    public Shared findById(int sharedId) {
        return sharedMapper.selectByPrimaryKey(sharedId);
    }

    /**
     * 查找单个分享，返回详细内容
     * @param sharedId
     * @return
     */
    @Override
    public DetailSharedVO findDetailById(int sharedId){
        Shared shared = sharedMapper.selectByPrimaryKey(sharedId);

        DetailSharedVO detailVo = new DetailSharedVO();
        detailVo.setSharedId(shared.getId());
        detailVo.setUserId(shared.getUserId());
        detailVo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
        detailVo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
        detailVo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
        detailVo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
        detailVo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
        detailVo.setCreateTime(shared.getCreateTime());
        // 设置commentList
        List<CommentTreeVO> commentTreeVOList = new ArrayList<>();
        List<SharedComment> fatherCommentList = sharedCommentMapper.selectFatherCommentBySharedId(sharedId);
        for (SharedComment fatherComment : fatherCommentList){
            CommentTreeVO treeVO = new CommentTreeVO();
            List<SharedComment> sonComments = sharedCommentMapper.selectByFatherId(fatherComment.getId());
            treeVO.setComment(fatherComment);
            treeVO.setSonCommentsList(sonComments);
            commentTreeVOList.add(treeVO);
        }
        detailVo.setCommentList(commentTreeVOList);

        return detailVo;
    }

    /**
     * 查找所有分享，以分页返回
     * @param page 当前页数
     * @param size 每页数量
     * @return
     */
    @Override
    public ResponsePageUtil<BriefSharedVO> findAllByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Shared> sharedList = sharedMapper.selectAll();


        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            // 检验当前shared的用户是否被禁言
            if (!checkSharedValid(shared)){
                continue;
            }
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<Shared> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedList);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }

    @Override
    public ResponsePageUtil<BriefSharedVO> findAllByLikedSum(int page, int size) {
        PageHelper.startPage(page, size);
        List<Object> sharedLikedsSumList = sharedLikedMapper.getAllByLikedSum();
        for (Object sharedLiked : sharedLikedsSumList){
            sharedLiked.toString();
        }
        return null;
    }

    @Override
    public ResponsePageUtil<BriefSharedVO> findAllByTime(int page, int size) {
        PageHelper.startPage(page, size);
        List<Shared> sharedList = sharedMapper.selectAllByTime();

        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            // 检验当前shared的用户是否被禁言
            if (!checkSharedValid(shared)){
                continue;
            }
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<Shared> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedList);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }

    @Override
    public List getSharedComment(int page, int size, int sharedId) {
        Shared shared = sharedMapper.selectByPrimaryKey(sharedId);

        // 设置commentList
        List<CommentTreeVO> commentTreeVOList = new ArrayList<>();
        List<SharedComment> fatherCommentList = sharedCommentMapper.selectFatherCommentBySharedId(sharedId);
        for (SharedComment fatherComment : fatherCommentList){
            CommentTreeVO treeVO = new CommentTreeVO();
            List<SharedComment> sonComments = sharedCommentMapper.selectByFatherId(fatherComment.getId());
            treeVO.setComment(fatherComment);
            treeVO.setSonCommentsList(sonComments);
            commentTreeVOList.add(treeVO);
        }

        return commentTreeVOList;
    }

    private boolean checkSharedValid(Shared shared){
        // 检查当前shared是否可见
        if (shared.getStatusId() == Const.SharedStatus.STATUS_INVISIBLE){
            return false;
        }
        // 检查发布shared的user是否可见
        UserInfo user = userInfoMapper.selectByPrimaryKey(shared.getUserId());
        if (user.getStatusId() == Const.UserStatus.STATUS_BAN){
            return false;
        }
        return true;
    }
}
